﻿// ***********************************************************************
// Assembly         : STX.Lib
// Author           : seven.x
// Created          : 12-29-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-29-2013
// ***********************************************************************
// <copyright file="MyContext.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using STX.Lib.Caching;
using STX.Lib.Caching.Providers;

namespace STX.Lib.Web
{
    /// <summary>
    /// Class MyHttpContext.
    /// </summary>
    public class MyHttpContext
    {

        /// <summary>
        /// The cache provider
        /// </summary>
        private static readonly ICacheProvider CacheProvider = new MemoryCacheProvider();

        /// <summary>
        /// Gets or sets the last timestamps.
        /// </summary>
        /// <value>The last timestamps.</value>
        private DateTime LastTimestamps { get; set; }

        /// <summary>
        /// Prevents a default instance of the <see cref="MyHttpContext"/> class from being created.
        /// </summary>
        private MyHttpContext()
        {
            this.LastTimestamps = DateTime.Now.AddHours(-1);
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static MyHttpContext Instance
        {
            get
            {
                var context =
                    CacheProvider.Get<MyHttpContext>("myHttpContext" + System.Web.HttpContext.Current.Session.SessionID);
                if (context == null)
                {
                    CacheProvider.Set("myHttpContext" + System.Web.HttpContext.Current.Session.SessionID,
                        new MyHttpContext());
                }
                return context;
            }
        }


        /// <summary>
        /// Gets the elapse seconds.
        /// </summary>
        /// <value>The elapse seconds.</value>
        public int ElapseSeconds
        {
            get
            {
                return (DateTime.Now - LastTimestamps).Seconds;
            }
        }

        /// <summary>
        /// Marks this instance.
        /// </summary>
        public void Mark()
        {
            this.LastTimestamps = DateTime.Now;
        }
    }
}
